package etnorservice.etnor_service.service;

import etnorservice.etnor_service.domain.po.FeedBack;
import etnorservice.etnor_service.domain.po.FeedbackDTO;
import etnorservice.etnor_service.mapper.FeedbackMapper;
import etnorservice.etnor_service.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2025/03/24/11:03
 **/
@Service
public class FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 提交反馈
     */
    /**
     * 提交反馈
     */
    public Result submitFeedback(FeedbackDTO feedback) {
        try {

            // 设置初始状态
            feedback.setStatus(0);  // 0-待处理

            // 插入数据库
            try {
                feedbackMapper.insertFeedback(feedback);
            }catch (Exception e){
                e.printStackTrace();
            }


            return Result.success("反馈提交成功");
        } catch (Exception e) {
            return Result.error("反馈提交失败：" + e.getMessage());
        }
    }

    /**
     * 更新反馈状态
     */
    public Result updateFeedbackStatus(Long feedbackId, Integer status) {
        try {
            feedbackMapper.updateFeedbackStatus(feedbackId, status);
            return Result.success("状态更新成功");
        } catch (Exception e) {
            return Result.error("状态更新失败：" + e.getMessage());
        }
    }

    /**
     * 查询反馈详情
     */
    public Result getFeedbackDetail(Long feedbackId) {
        try {
            FeedbackDTO feedback = feedbackMapper.getFeedbackById(feedbackId);
            Result result = new Result();
            result.setData(feedback);
            return result;
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    public FeedbackDTO getFeedbackById(Long prombleId){
        return  feedbackMapper.getFeedbackById(prombleId);
    }

    /**
     * 查询用户的所有反馈
     */
    public Result getUserFeedbacks(Long userId) {
        try {
            List<FeedbackDTO> feedbacks = feedbackMapper.getFeedbacksByUserId(userId);
            Result result = new Result();
            result.setData(feedbacks);
            return result;
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取反馈列表（带分页和筛选）
     */
    public Result getFeedbackList(Integer page, Integer pageSize, Integer status, String keyword) {
        try {
            // 计算偏移量
            int offset = (page - 1) * pageSize;

            // 获取分页数据
            List<FeedbackDTO> feedbacks = feedbackMapper.getFeedbackList(offset, pageSize, status, keyword);

            // 获取总数
            int total = feedbackMapper.getFeedbackCount(status, keyword);

            // 构建返回结果
            Result result = new Result();
            result.setData(new HashMap<String, Object>() {{
                put("list", feedbacks);
                put("total", total);
            }});

            return result;
        } catch (Exception e) {
            return Result.error("获取反馈列表失败：" + e.getMessage());
        }
    }
}